package sorts;

public class Code_75 {
    public void sortColors(int[] nums) {
        if (nums == null || nums.length < 2) return;

        for (int i = 1; i < nums.length; i++) {
            for (int j = i - 1; j >= 0 && nums[j] > nums[j + 1]; j--) {
                swap(nums, j, j + 1);
            }
        }
    }

    /**
     * 解法二
     *
     * @param nums
     */
    public void sortColors2(int[] nums) {
        if (nums == null || nums.length < 2) return;

        int[] res = new int[3];
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == 0) res[0]++;
            if (nums[i] == 1) res[1]++;
            if (nums[i] == 2) res[2]++;
        }

    }

    private void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}
